<?php
/**
 * 系统道具管理模块
 *
 * @author river_he@eueang.com
 */
final class Kit_Model_Property extends Kit_Model_Abstract{
	
	/**
	 * 销售道具
	 * 
	 * @param int $shoperId 店主id
	 * @param int $shoperPropId 店道具id 
	 * @return int 1:OK -1:no property -2:shoper wrong -3:prope already used -4:no shoper 
	 */
	public function sell($shoperId, $shoperPropId){
		$shopProp = $this->getShoperPropDao()->getItem('id='.$shoperPropId);
		if(!$shopProp){
			return -1; 
		}elseif($shopProp['nUid'] != $shoperId){
			return -2;
		}elseif(1 != $shopProp['nStatus']){
			return -3;
		}
		$shoper = $this->getShoperDao()->getItem('id='.$shopProp['nUid']);
		if(!$shoper){
			return -4;
		}
		$set = array();
		$pay = $shopProp['nPrice'];
		if(0 < $pay){
			if(1 == $shopProp['nPriceType']){
				$pay += $shoper['nGold'];
				$set['nGold'] = $pay;
			}elseif(2 == $shopProp['nPriceType']){
				$pay += $shopProp['nKb'];
				$set['nKb'] = $pay;
			}
		}
		if($set){
			if(!$this->getShoperDao()->update('id='.$shoper['id'], $set)){
				return -5;
			}
		}
		if($this->getShoperPropDao()->update('id='.$shopProp['id'], array('nStatus'=>3))){
			return 1;
		}
		return -6;
	}
	
	/**
	 * 购买道具
	 * 
	 * @param int $shoperId
	 * @param int $propId
	 * @param int $total
	 * @return int|array
	 */
	public function buy($shoperId, $propId, $total){
		$property = $this->getPropertyDao()->getItem('id='.$propId);
		if(!$property){
			return -1;
		}
		$shoper = $this->getShoperDao()->getItem('id='.$shoperId);
		if(!$shoper){
			return -2; 
		}
		$pay = $property['nPrice']*$total;
		if(0 > $pay){
			return -3;
		}
		if(1 > $property['nRebate']){
			$pay = ceil($pay*$property['nRebate']);
		}
		$set = array();
		if (0 < $pay){
			if(1 == $property['nPriceType']){
				if($shoper['nGold'] < $pay){
					return -4;
				}else{
					$set['nGold'] = $shoper['nGold']-$pay;
				}
			}else if(2 == $property['nPriceType']){
				if($shoper['nKb'] < $pay){
					return -5;
				}else{
					$set['nKb'] = $shoper['nKb']-$pay;
				}
			}
			//@todo 事务处理
			if($set){
				if(!$this->getShoperDao()->update('id='.$shoper['id'], $set)){
					return -6;
				}
			}
		}
		$hasWhere = array('nUid='.$shoper['id']);
		array_push($hasWhere, 'nPid='.$property['id']);
		$hasProp = $this->getShoperPropDao()->getItem($hasWhere);
		if($hasProp){
			$hasProp['nTotal'] += $total;
			$this->getShoperPropDao()->update('id='.$hasProp['id'], array('nTotal'=>$hasProp['nTotal']));
			$this->getPropertyDao()->update('id='.$property['id'], array('nSells'=>$property['nSells']+1));
			return $hasProp;
		}else{
			$row = array(
			'nUid'=>$shoper['id'],
			'nPid'=>$property['id'],
			'sName'=>$property['sName'],
			'nTotal'=>$total,
			'nCrTime'=>Kit_Util::getTimeStamp()
			);
			if($id = $this->getShoperPropDao()->add($row)){
				$this->getPropertyDao()->update('id='.$property['id'], array('nSells'=>$property['nSells']+1));
				$row['id'] = $id;
				return $row;
			}
		}
		return -7;
	}
	
	/**
	 * 获取多条道具记录
	 * 
	 * @param array|string $where
	 * @param string $sort
	 * @param int $page
	 * @param int $psize
	 * @return array
	 */
	public function getItems($where, $sort='', $page=1, $psize=10){
		$return = array();
		$data = $this->getPropertyDao()->getItems($where, $sort, $page, $psize);
		if($data){
			$psize = $psize>0?$psize:10;
			$page = $page>0?$page:1;
			$return['data'] = $data;
			$total = $this->getPropertyDao()->getWhereTotal($where);
			$return['pager'] = array(
			'rtotal'=>$total,
			'page'=>$page,
			'psize'=>$psize,
			'ptotal'=>ceil($total/$psize)
			);
		}
		return $return;
	}
}
//end